'use strict';

const Controller = require('egg').Controller;

let jwt = require("jsonwebtoken")

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //美洽首页
  async Homelist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('homelist')
    const { authorization } = ctx.request.header
    // console.log(authorization);
    try {
      //jwt
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 200,
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
      }
    }
  }


  //my 我的页面 移动端 信息渲染
  async Mylogin() {
    const { ctx, app } = this;
    // login 数据接口
    const data = await app.mysql.select('login')
    // ctx.body = {
    //   code: 200,
    //   data,
    // }
    const { authorization } = ctx.request.header
    try {
      //jwt
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 200,
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
      }
    }
  }

  //移动端账号 删除
  async dellogin() {
    const { ctx, app } = this
    const { id } = ctx.request.body;
    const data = await app.mysql.delete('login', { id })
    ctx.body = {
      data,
      code: 200
    }
  }

  //商城订单
  async shop() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shopcar')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 200,
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
      }
    }
  }
  async shopdel() {
    const { ctx, app } = this;
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('shopcar', { id })
    ctx.body = {
      code: 200
    }
  }
  async shopsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from shopcar where title like "%${value}%"`)
    ctx.body = {
      data
    }

  }
  async shopexit() {
    const { ctx, app } = this
    const { title, id, price } = ctx.request.body
    await app.mysql.update('shopcar', { id, title, price })
    ctx.body = {
      code: 200,
    }
  }
  async shopadd() {
    const { ctx, app } = this
    const { title, price } = ctx.request.body
    console.log(title);
    const img = 'https://img.alicdn.com/imgextra/i1/2928278102/O1CN01U9z5Aq29ilddHhQrs_!!2928278102-0-sm.jpg_430x430q90.jpg'
    await app.mysql.insert('shopcar', { title, price, img, num: 1 })
    ctx.body = {
      code: 200
    }
  }



  //商城列表

  async shoplist() {
    const { ctx, app } = this;
    const data = await app.mysql.select('shoplist')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 200,
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
      }
    }
  }
  async shoplistdel() {
    const { ctx, app } = this;
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('shoplist', { id })
    ctx.body = {
      code: 200
    }
  }
  async shoplistsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from shoplist where title like "%${value}%"`)
    ctx.body = {
      data
    }

  }
  async shoplistexit() {
    const { ctx, app } = this
    const { title, id, price } = ctx.request.body
    await app.mysql.update('shoplist', { id, title, price })
    ctx.body = {
      code: 200,
    }
  }
  async shoplistadd() {
    const { ctx, app } = this
    const { title, price } = ctx.request.body
    console.log(title);
    const img = 'https://img.alicdn.com/imgextra/i1/2928278102/O1CN01U9z5Aq29ilddHhQrs_!!2928278102-0-sm.jpg_430x430q90.jpg'
    await app.mysql.insert('shoplist', { title, price, img, num: 0 })
    ctx.body = {
      code: 200
    }
  }



  //科普



  async list() {
    const { ctx, app } = this;
    const data = await app.mysql.select('list')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 200,
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 500,
      }
    }
  }
  async listdel() {
    const { ctx, app } = this;
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('list', { id })
    ctx.body = {
      code: 200
    }
  }
  async listsearch() {
    const { ctx, app } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from list where title like "%${value}%"`)
    ctx.body = {
      data
    }

  }
  async listexit() {
    const { ctx, app } = this
    const { title, time, type, id } = ctx.request.body
    await app.mysql.update('list', { id, title, time, type })
    ctx.body = {
      code: 200,
    }
  }
  async listadd() {
    const { ctx, app } = this
    const { title, time, type } = ctx.request.body

    const img = 'https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00198-1568.jpg'
    await app.mysql.insert('list', { title, time, type, img })
    ctx.body = {
      code: 200
    }
  }




}

module.exports = HomeController;
